package com.pxsw.apkfactory.datamanage;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Zip {
	private static final int BUFFER = 2048;
	
	public void zip(InputStream in, OutputStream out,String name) throws IOException {
		BufferedInputStream origin = null;
		ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(out));
		byte data[] = new byte[BUFFER];
		origin = new BufferedInputStream(in, BUFFER);
		ZipEntry entry = new ZipEntry(name);
		zipout.putNextEntry(entry);
		int count;
		while ((count = origin.read(data, 0, BUFFER)) != -1) {
			zipout.write(data, 0, count);
		}
		origin.close();
		zipout.close();
		in.close();
	}
	
	public void zip(InputStream in, OutputStream out) throws IOException {
		this.zip(in, out, "retn");
	}
	

	public void unzip(String path,OutputStream out) throws IOException{
		unzip(new File(path),out);
	}
	
	public void unzip(File file, OutputStream out) throws IOException {
		unzip(new FileInputStream(file),out);
	}
	
	public void unzip(InputStream in,OutputStream out) throws IOException{
		ZipInputStream zin = new ZipInputStream(in);
		while ((zin.getNextEntry()) != null) {
			BufferedInputStream bis = new BufferedInputStream(zin);
			BufferedOutputStream bos = new BufferedOutputStream(out, BUFFER);
			byte[] buf = new byte[BUFFER];
			int count;
			while ((count = bis.read(buf, 0, BUFFER)) != -1) {
				bos.write(buf, 0, count);
			}
			bos.close();
			bis.close();
			out.close();
			break;
		}
		zin.close();
		in.close();
	}
}
